package com.gs.api.util.video.parse;

import cn.hutool.core.map.MapUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.CRC32;

/**
 * @author guoyunfei on 2022/3/12 14:18
 * @className ParseUtil
 * @description TODO 解析工具
 * @Version 1.0
 */
public class ParseUtil {


    /**
     * 方法描述: 过滤分享链接的中文汉字
     */
    public static String filterUrl(String url) {
        String regex = "https?://(\\w|-)+(\\.(\\w|-)+)+(/(\\w+(\\?(\\w+=(\\w|%|-)*(\\&\\w+=(\\w|%|-)*)*)?)?)?)+";//匹配网址
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(url);
        if(m.find()){
            return  url.substring(m.start(),m.end());
        }
        return "";
    }


   public static String resp(String url){
        try {
            Connection con= Jsoup.connect(url);
            con.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            con.header("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
            con.header("Accept-Encoding", "gzip, deflate, sdch, br");
            con.header("Accept-Language", "zh-cn,zh;q=0.8");
            con.header("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16D57 Version/12.0 Safari/604.1");
            Connection.Response resp= con.method(Connection.Method.GET).execute();
            return con.method(Connection.Method.GET).execute().url().toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    // 定义请求头
    public static Map<String, String> headers(){
        Map<String, String> headers = MapUtil.newHashMap();
        headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        headers.put("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
        headers.put("Accept-Encoding", "gzip, deflate, sdch, br");
        headers.put("Accept-Language", "zh-cn,zh;q=0.8");
        headers.put("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 12_1_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16D57 Version/12.0 Safari/604.1");

        return headers;
    }

    //生成随机数
    public static String getRandom(int count) {
        Random random = new Random();
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < count; i++) {
            result.append(random.nextInt(10));
        }
        return result.toString();
    }




}
